var m_order = {};

m_order.param = {
		flag:"all",
		status:"all",
		num:null,
		startTime:"",
		endTime:"",
		page:1,
		rows:9
}

m_order.getOrderCount = function() {
	var req = new common.Request();
	var url = "../orderInfo/groupFlag.action";
	var reqData = {};
	req.sendMethodRequest("post", url, reqData, function(e){
		if (e != null && e.isSuccess && e.flagList != null && e.flagList.length > 0) {
			var dataObj = {};
			var total = 0;
			for (var i = 0; i < e.flagList.length; i ++) {
				var obj = e.flagList[i];
				if (obj.hasOwnProperty('num') && obj.num != null) {
					total = total + obj.num;
					dataObj[obj.flag] = obj.num;
				}
			}
			dataObj['all'] = total;
			var item_con = $('.content .left .item_con');
			for (var i = 0; i < item_con.length; i ++) {
				var text = $(item_con[i]).html();
				if (dataObj.hasOwnProperty($(item_con[i]).attr('value'))) {
					$(item_con[i]).html(text + "(" + dataObj[$(item_con[i]).attr('value')] + ")");
				} else {
					$(item_con[i]).html(text + "(0)");
				}
			}
		}
	}, function(e){
		
	});
}

m_order.getStatusCount = function() {
	var req = new common.Request();
	var url = "../orderInfo/groupStatus.action";
	var reqData = {};
	if (m_order.param.flag != null && m_order.param.flag != "all") {
		reqData['flag'] = m_order.param.flag;
	}
	req.sendMethodRequest("post", url, reqData, function(e){
		if (e != null && e.isSuccess && e.statusList != null && e.statusList.length > 0) {
			var dataObj = {};
			var total = 0;
			for (var i = 0; i < e.statusList.length; i ++) {
				var obj = e.statusList[i];
				if (obj.hasOwnProperty('num') && obj.num != null) {
					total = total + obj.num;
					dataObj[obj.status] = obj.num;
				}
			}
			dataObj['0'] = total;
			dataObj['all'] = total;
			var item_con = $('.content .right .status ul li');
			for (var i = 0; i < item_con.length; i ++) {
				var text = $(item_con[i]).html();
				if (text.indexOf("(") > -1) {
					text = text.split('(')[0];
				}
				if (dataObj.hasOwnProperty($(item_con[i]).attr('value'))) {
					$(item_con[i]).html(text + "(" + dataObj[$(item_con[i]).attr('value')] + ")");
				} else {
					$(item_con[i]).html(text + "(0)");
				}
			}
		}
	}, function(e) {
		
	});
}

m_order.itemItem = function() {
	$('.item_con').click(function(){
		$('.item_con').removeClass('select');
		$(this).addClass('select');
		m_order.param.flag = $(this).attr('value');
		m_order.param.page = 1;
		m_order.getStatusCount();
		m_order.getData();
	});
}

m_order.statusInit = function() {
	$('.content .right .status ul li').click(function(){
		var lis = $('.content .right ul li');
		for (var i = 0; i < lis.length; i ++) {
			$(lis[i]).removeClass('select');
		}
		$(this).addClass('select');
		m_order.param.status = $(this).attr('value');
		m_order.param.page = 1;
		m_order.getData();
	});
}

m_order.pageInit = function(pageNum, page, pageChangeBack) {
	$('#pagination').empty();
    var jqPaginator = $.jqPaginator('#pagination', {
        totalPages: pageNum,
        visiblePages: 5,
        currentPage: page,
        first:'<li class="first" jp-role="first" jp-data="1"><a href="javascript:;">首页</a></li>',
        prev: '<li class="prev"><a href="javascript:;">上一页</a></li>',
        next: '<li class="next"><a href="javascript:;">下一页</a></li>',
        last:'<li class="last" jp-role="last" jp-data="100"><a href="javascript:;">尾页</a></li>',
        page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
        //onPageChange: function (num, type) {
         //   $('#p1').text(type + '：' + num);
        //}
        onPageChange: pageChangeBack
    });
}

m_order.getData = function() {
	var url = "../orderInfo/findOrderInfoList.action";
	var req = new common.Request();
	var reqData = {};
	$('#spread_list .data_tr').remove();
	for (var key in m_order.param) {
		if (m_order.param[key] != null && m_order.param[key] != "") {
			if (key == "flag") {
				if (m_order.param[key] != "all") {
					reqData[key] = m_order.param[key];
				}
			} else if (key == "status") {
				if (m_order.param[key] != "all") {
					reqData[key] = m_order.param[key];
				}
			} else {
				reqData[key] = m_order.param[key];
			}
		}
	}
	console.log()
	req.sendRequest(url, reqData, function(e){
		if (e != null && e.isSuccess) {
			var spread_list = $('#spread_list');
			for (var i = 0; i < e.info.length; i ++) {
				(function(i){
					var info = e.info[i];
					var emptyTr = $('<tr></tr>').addClass('data_tr').appendTo(spread_list);
					var emptyTd1 = $('<td></td>').appendTo(emptyTr);
					var emptyTd2 = $('<td></td>').appendTo(emptyTr);
					var emptyTd3 = $('<td></td>').appendTo(emptyTr);
					var emptyTd4 = $('<td></td>').appendTo(emptyTr);
					var emptyTd5 = $('<td></td>').appendTo(emptyTr);
					var emptyTd6 = $('<td></td>').appendTo(emptyTr);
					var emptyTd7 = $('<td></td>').appendTo(emptyTr);
					var dataTr = $('<tr></tr>').addClass('data_tr').appendTo(spread_list);
					var dataTd1 = $('<td></td>').html(info.num == null ? "" : info.num).appendTo(dataTr);
					var dataTd2 = $('<td></td>').html(info.name == null ? "" : info.name).appendTo(dataTr);
					var dataTd3 = $('<td></td>').html((info.price == null ? 0 : info.price) + "元").appendTo(dataTr);
					var flagStr = "";
					if (info.flag = 1) {
						flagStr = "快写";
					} else if (info.flag = 2) {
						flagStr = "快发";
					} else if (info.flag = 3) {
						flagStr = "快画";
					} else if (info.flag = 4) {
						flagStr = "快采";
					}
					var dataTd4 = $('<td></td>').html(flagStr).appendTo(dataTr);
					var createTime = "";
					if (info.createTime != null) {
						var cateDate = new Date(info.createTime);
						createTime = cateDate.getFullYear()+"-"+(cateDate.getMonth()+1)+"-"+cateDate.getDate();
					}
					var dataTd6 = $('<td></td>').html(createTime).appendTo(dataTr);
					var statusStr = "";
					if (info.flag = 1) {
						flagStr = "提交任务";
					} else if (info.flag = 2) {
						flagStr = "待确认";
					} else if (info.flag = 3) {
						flagStr = "待付款";
					} else if (info.flag = 4) {
						flagStr = "待完成";
					} else if (info.flag = 5) {
						flagStr = "已完成";
					} else if (info.flag = 6) {
						flagStr = "已交付";
					}
					var dataTd7 = $('<td></td>').html(flagStr).appendTo(dataTr);
					var dataTd5 = $('<td></td>').appendTo(dataTr);
					var ziliao = $('<a></a>').html("修改任务 |").appendTo(dataTd5).click(function() {
						
					});
					var renwen = $('<a></a>').html(" 接受任务 ").appendTo(dataTd5).click(function() {
						
					});
				})(i);
			}
			var init = false;
			m_order.pageInit(e.sum, m_order.param.page, function(num, type){
				m_order.param.page = num;
				if (init) {
					m_order.getData();
				} else {
					init = true;
				}
			});
		}
	}, function(e){
		
	});
}

m_order.init = function() {
	m_order.getData();
	m_order.itemItem();
	m_order.statusInit();
	m_order.getOrderCount();
	m_order.getStatusCount();
}

$(document).ready(function() {
	m_order.init();
});